/*
 * Copyright (C) 2010 Fred Bojin Liu.
 *
 * Licensed under the GPL, Version 2.0;
 * you may not use this file except in compliance with the License.
 * A copy of the license is included (see the "LICENSE.txt")
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.ucdavis.fredliu.speedtracker;

import java.util.ArrayList;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MocGPS extends MapActivity {
	public static String TAG = "MocGPS";
	/** Called when the activity is first created. */
	MapView mMapView;
	/** The chart view to be added*/
	GraphicalView mChartView;
	/** Dataset for time and speed */
	XYMultipleSeriesDataset dataset;
	
	MapController mMapController;
	LocationManager mLocationManager;
	CarItemizedOverlay itemizedOverlay;
	/**
	 * Storying the trace record, passed into and updated by the
	 * LocationUpdateHandler
	 */
	ArrayList<Record> trace = new ArrayList<Record>();
	/** File manager that holds the trace file */
	FileManager fileManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// this.findViewById(R.id.mapview)
		setContentView(R.layout.main);
		if (fileManager == null) {
			fileManager = new FileManager("trace");
			fileManager.createFile();
		}
		//get the MapView
		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setClickable(false);
		mMapController = (MapController) mMapView.getController();
		//setup overlay items
		Drawable car = this.getResources().getDrawable(R.drawable.car);
		itemizedOverlay = new CarItemizedOverlay(car);
		mMapView.getOverlays().add(itemizedOverlay);
		//location manager, requesting updates and providing location update handler
		mLocationManager = (LocationManager) this
				.getSystemService(LOCATION_SERVICE);
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				0, 0, new LocationUpdateHandler(mMapController,
						itemizedOverlay, this));
	}
	@Override
	public void onResume(){
		super.onResume();
		Log.d(TAG, "Before onResume is called");
		if(mChartView==null){
			dataset = new XYMultipleSeriesDataset();
			XYSeries series = new XYSeries("Trajectory");
			dataset.addSeries(series);
			
			LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
			mChartView = ChartFactory.getLineChartView(this, dataset, getRenderer());
			layout.addView(mChartView, new LayoutParams(310,130));
			
		}else{
			mChartView.repaint();
		}
		
		
	}
	@Override
	public void onConfigurationChanged (Configuration newConfig){
		super.onConfigurationChanged(newConfig);
//		int i = trace.size();
	}
	
	private XYMultipleSeriesRenderer getRenderer() {
	       XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); 
	       XYSeriesRenderer r = new XYSeriesRenderer();
	       r.setColor(Color.GREEN);
	       r.setPointStyle(PointStyle.DIAMOND);
	       r.setFillBelowLine(true);
	       r.setFillBelowLineColor(Color.WHITE);
	       r.setFillPoints(true);
	    
	       renderer.addSeriesRenderer(r);
//	       r = new XYSeriesRenderer();
//	       r.setPointStyle(PointStyle.CIRCLE);
//	       r.setColor(Color.GREEN);
//	       r.setFillPoints(true);
//	       renderer.addSeriesRenderer(r);
	       renderer.setAxesColor(Color.WHITE);
	       renderer.setLabelsColor(Color.LTGRAY);
	       renderer.setXTitle("Time (s)");
	       renderer.setYTitle("Speed (mph)");
	       return renderer;
	     } 
	
	public ArrayList<Record> getTrace() {
		return trace;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}